package logic

import (
	"cart_srv/cart"
	"context"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCartAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartAddLogic {
	return &CartAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CartAddLogic) CartAdd(req *types.CartAddrequest) (resp *types.Response, err error) {
	add, err := l.svcCtx.Cartrpc.CartAdd(l.ctx, &cart.CartAddRequest{
		UserId:  int64(req.UserId),
		GoodsId: int64(req.GoodsId),
		Num:     int64(req.Num),
	})
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}

	return &types.Response{
		Code:    200,
		Message: "购物车添加成功",
		Data:    add,
	}, nil
}
